/*
 * SdMod.c
 *
 *  Created on: 2020-04-30
 *      Author: mxw
 */
#include <SdMod.h>
#include <SdMmcDrv.h>
#include <commlib.h>
#define SD_MOD_BUFF_LEN  2048

#define SDCARD_BUFF_SIZE    512

CModule gSdMod;
static char _sd_mod_buf[SD_MOD_BUFF_LEN] = {0};

unsigned char cRxBuffer[SDCARD_BUFF_SIZE];
extern void Test_SDI(void);

static void test_sd() {
	unsigned short i;
	Test_SDI();
	return;
    for (i = 0; i < SDCARD_BUFF_SIZE; i++) {
        cRxBuffer[i] = 0x00;
    }

    if (Read_One_Block(4096, (unsigned int *) cRxBuffer)) {
        for (i = 0; i < 512;) {
            printf("%02x ", cRxBuffer[i + 3]);
            printf("%02x ", cRxBuffer[i + 2]);
            printf("%02x ", cRxBuffer[i + 1]);
            printf("%02x ", cRxBuffer[i + 0]);
            i += 4;
            if (i % 16 == 0)
                printf("\n");
        }
        printf("\nRead 4096 Block is OK!\n");
    } else {
        printf("\nRead 4096 Block is Fail!\n");
    }
}

static int on_sd_mod_recv_msg(struct _cmodule_* thes, CMsg* msg) {
    switch(msg->msg_id) {
    case MSG_ID_TEST_SD_REQ:
        test_sd();
        return 0;
    }
    printf("[Err] sd module recv msg:0x%x, data len:%d, data[0]=%c\r\n", msg->msg_id, msg->data_len, msg->data[0]);    
    return 0;
}

int init_sd_module() {
    construct_module(&gSdMod, "sd r/w module", on_sd_mod_recv_msg);
    gSdMod.q.set_queue_buf(&gSdMod.q, _sd_mod_buf, SD_MOD_BUFF_LEN);
    gSdMod.run(&gSdMod);
	return 0;
	if(0 == SDinit()) {
		printf("SD init FAILED!\r\n");
	}
	else {
		printf("SD init OK.\r\n");
	}
    return 0;
}

